﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Common;
using Topshelf;

namespace WindowsService_Topshelf.Services
{
    /// <summary>
    /// Topshelf安装Windows服务，更加稳定
    /// </summary>
    public sealed class ServiceRunnerOne : ServiceControl, ServiceSuspend
    {
        private string ServiceName { get; set; }
        /// <summary>
        /// 使用标记来控制运行，也可以使用Quartz.Net
        /// </summary>
        private bool Flag { get; set; }

        public ServiceRunnerOne()
        {
            try
            {
                Flag = false;
                ServiceName = "测试服务1";
                var task = Task.Run(() => TestService("入参"));
            }
            catch (Exception ex)//此处不能捕获到Task的异常，需要Task内部捕获
            {
                LogHelper.Error("服务【" + ServiceName + "】构造函数异常：" + ex.Message);
            }
            
        }

        public bool Start(HostControl hostControl)
        {
            try
            {
                Flag = true;
            }
            catch (Exception ex)
            {
                LogHelper.Error("服务【"+ServiceName+"】启动异常："+ex.Message);
            }

            return true;
        }

        public bool Stop(HostControl hostControl)
        {
            try
            {
                Flag = false;
            }
            catch (Exception ex)
            {
                LogHelper.Error("服务【" + ServiceName + "】停止异常：" + ex.Message);
            }

            return true;
        }

        public bool Continue(HostControl hostControl)
        {
            try
            {
                Flag = true;
            }
            catch (Exception ex)
            {
                LogHelper.Error("服务【" + ServiceName + "】继续运行异常：" + ex.Message);
            }

            return true;
        }

        public bool Pause(HostControl hostControl)
        {
            try
            {
                Flag = false;
            }
            catch (Exception ex)
            {
                LogHelper.Error("服务【" + ServiceName + "】暂停异常：" + ex.Message);
            }

            return true;
        }

        #region 私有方法
        /// <summary>
        /// 测试服务
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        private void TestService(string param)
        {
            while (true)
            {
                try
                {
                    if (Flag)
                    {
                        LogHelper.Debug("测试服务执行中...");
                        Thread.Sleep(3000);
                        LogHelper.Debug("测试服务执行结束...");
                    }
                    else
                    {
                        LogHelper.Debug("测试服务已暂停...");
                        Thread.Sleep(30000);
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error("服务【" + ServiceName + "】执行异常：" + ex.Message);
                    Thread.Sleep(5000);//睡眠5秒，防止某些异常导致无限输出日志
                }
            }
        }
        #endregion
    }
}
